﻿package com.wly
{
	import com.wly.utils.PNGEncoder;
	import com.wly.utils.RemoteResource;
	import com.wly.utils.TGADecoder;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.net.FileFilter;
	import flash.text.TextField;
	import flash.utils.ByteArray;
	
	public class Tga2PngMain extends Sprite
	{
		private var _file:File;
		private var _fileArr:Array = [];
		
		private var _btnFileSelect:SimpleButton;
		private var _btnDirSelect:SimpleButton;
		private var _btnTranslate:SimpleButton;
		private var _tfStat:TextField;
		private var _btnSplit:SimpleButton;
		private var _tfWidthNum:TextField;
		private var _tfHeightNum:TextField;
		
		private var _fileName:String;
		private var _stat:int = 0;
		private const STAT_FILE:int = 1;
		private const STAT_DIR:int = 2;
		private var _curIndex:int;
		
		
		private var SPLIT_HEIGHT_NUM:int;
		private var SPLIT_WIDTH_NUM:int;
		
		public function Tga2PngMain()
		{
			super();
			
			_btnFileSelect = getChildByName("btnFileSelect") as SimpleButton;
			_btnDirSelect  = getChildByName("btnDirSelect") as SimpleButton;
			_btnTranslate  = getChildByName("btnTranslate") as SimpleButton;
			_tfStat        = getChildByName("tfStat") as TextField;
			_btnSplit      = getChildByName("btnSplit") as SimpleButton;
			_tfWidthNum    = getChildByName("tfWidthNum") as TextField;
			_tfHeightNum   = getChildByName("tfHeightNum") as TextField;
			_tfWidthNum.restrict = "0-9";
			_tfHeightNum.restrict= "0-9";
			
			_btnFileSelect.addEventListener(MouseEvent.CLICK, onFileSelect);
			_btnDirSelect.addEventListener(MouseEvent.CLICK, onDirSelect);
			_btnTranslate.addEventListener(MouseEvent.CLICK, onTranslate);
			_btnSplit.addEventListener(MouseEvent.CLICK, onSplit);
		}
		
		private function onFileSelect(e:MouseEvent):void
		{
			_file = File.desktopDirectory;
			_file.addEventListener(Event.SELECT, onFileSelected);
			_file.browseForOpen("tgaFile",[new FileFilter("tgaFile","*.tga;*.png")]);
		}
		
		private function onFileSelected(e:Event):void
		{
			_fileArr = [];
			_fileArr.push(_file);
			_file.removeEventListener(Event.SELECT, onFileSelected);
			_stat = STAT_FILE;
			_fileName = _file.nativePath;
			_tfStat.text = "文件总数：1";
		}
		
		private function onDirSelect(e:MouseEvent):void
		{
			_file = File.desktopDirectory;
			_file.addEventListener(Event.SELECT, onFileDirSelected);
			_file.browseForDirectory("tga directory");
		}
		
		private function onFileDirSelected(e:Event):void
		{
			var _fileArrTmp:Array = [];
			var i:int;
			var file:File;
			var name:String;
			var pe:RegExp = /tga$|png$/;
			
			_file.removeEventListener(Event.SELECT, onFileDirSelected);
			_fileArr = [];
			_fileArrTmp = _file.getDirectoryListing();
			for(i=0; i<_fileArrTmp.length; i++)
			{
				file = _fileArrTmp[i] as File;
				name = file.name;
				if(name.match(pe))
				{
					_fileArr.push(file);
				}
			}
			_tfStat.text = "文件总数："+_fileArr.length.toString();
		}
		
		private function onTranslate(e:MouseEvent):void
		{
			var i:int;
			var file:File;
			var bmd:BitmapData;
			var fs:FileStream = new FileStream;
			var byteArr:ByteArray;
			var pe:RegExp = /tga$/;
			
			for(i=0; i<_fileArr.length; i++)
			{
				_curIndex = i;
				_tfStat.text = "当前状态："+(_curIndex+1).toString()+"/"+_fileArr.length.toString();
				file = _fileArr[i] as File;
				if(file.name.match(pe))
				{
					byteArr = new ByteArray;
					fs.open(file, FileMode.READ);
					fs.readBytes(byteArr,0)
					bmd  = tgaToBitmapData(byteArr);
					doTranslateBmd2Png(bmd);
				}
			}
		}

		private function tgaToBitmapData(bytes:ByteArray):BitmapData
		{
			var tgaDecoder:TGADecoder = new TGADecoder(bytes);
			return tgaDecoder.bitmap;
		}
		
		private function doTranslateBmd2Png(bmd:BitmapData):void
		{
			var fs:FileStream;
			var file:File;
			var curFile:File = _fileArr[_curIndex] as File;
			var path:String = curFile.nativePath;
			var pat:RegExp = /tga$/;
			path = path.replace(pat, "png");

			if(bmd)
			{
				file = new File(path);
				fs = new FileStream();
				fs.open(file, FileMode.WRITE);
				fs.writeBytes(PNGEncoder.encode(bmd), 0);
				fs.close();
			}
		}
		
		private function onSplit(e:MouseEvent):void
		{
			var i:int;
			var file:File;
			var pe:RegExp = /png$/;
			
			SPLIT_HEIGHT_NUM = int(_tfHeightNum.text);
			SPLIT_WIDTH_NUM  = int(_tfWidthNum.text);
			for(i=0; i<_fileArr.length; i++)
			{
				_curIndex = i;
				_tfStat.text = "当前状态："+(_curIndex+1).toString()+"/"+_fileArr.length.toString();
				file = _fileArr[i] as File;
				if(file && file.name.match(pe))
				{
					RemoteResource.getInstance().requestRs(file.nativePath,doSplitBitMapData);
				}
			}
		}
		
		private function doSplitBitMapData(data:*):void
		{
			var bmp:Bitmap = data as Bitmap;
			var i:int;
			var j:int;
			var subwidth:int;
			var subheight:int;
			var bmd:BitmapData;
			var subBmd:BitmapData;
			var fs:FileStream;
			var rect:Rectangle;
			var file:File;
			var curFile:File = _fileArr[_curIndex] as File;
			var path:String;
			var pat:RegExp = /.png$/;
			var newNameSubStr:String;
			
			if(bmp)
			{ 
				bmd  = bmp.bitmapData;
				subwidth  = bmd.width/SPLIT_WIDTH_NUM;
				subheight = bmd.height/SPLIT_HEIGHT_NUM;
				rect = new Rectangle(0, 0, subwidth, subheight);
				for(i=0; i<SPLIT_HEIGHT_NUM; i++)
				{
					for(j=0; j<SPLIT_WIDTH_NUM; j++)
					{
						subBmd = new BitmapData(subwidth, subheight);
						rect.x = subwidth*j;
						rect.y = subheight*i;
						subBmd.copyPixels(bmd, rect, new Point);
						path = curFile.nativePath;
						newNameSubStr = j.toString()+i.toString()+".png";
						path = path.replace(pat, newNameSubStr);
						file = new File(path);
						fs   = new FileStream();
						fs.open(file, FileMode.WRITE);
						fs.writeBytes(PNGEncoder.encode(subBmd), 0);
						fs.close();
					}
				}

			}
		}
	}
}